package voidgame.core;
import java.io.File;
import java.util.ArrayList;
import java.util.Random;

public class Planet extends CelestialObject{
	
	ArrayList<Moon> moons;
	
	StarSystem system;
	
	String name;
	int type;
	final String[] PLANETTYPE = {"Terrestial","Gas Giant"};
	
	int dist;
	int temp; //very hot, hot, med, cold, very cold
	final String[] TEMP = {"very hot", "hot", "", "cold", "very cold"};
	int size;	//tiny, small, med, large // small, med, huge, massive
	final String[] SIZE = {"tiny", "small", "", "large", "small", "", "huge", "massive"};
	
	int rings;
	boolean life;
	String ringSprite;
		
	String desc;
	
	public Planet(String n, int d, StarSystem s){
		
		Random ran = new Random();
		
		name = n;
		dist = d;
		system = s;
		
		pos = new int[2];
		
		temp =  new Double(Math.floor((double)d/(double)2)).intValue();
		
		
		if(system.star.temp==0)
			temp++;
		if(system.star.temp==2)
			temp--;
		
		if(temp<0)temp=0; 
		if(temp>4)temp=4; 
		
		
		size = ran.nextInt(4);
		
		type = ran.nextInt(2);
		if(type==1){
			size=size+4;
			rings=ran.nextInt(3);
		}
		
		if(rings==1)
			ringSprite=".//images//celestial//other//1ring"+(ran.nextInt(4)+1)+".png";
		if(rings==2)
			ringSprite=".//images//celestial//other//2ring"+(ran.nextInt(4)+1)+".png";
		
		life=false;
		if(type==0&&temp==3&&system.star.startype>1&&ran.nextInt(2)==0){
			life=true;
		}
		
		//int[] moondist = {3,4,5,6,9,15,25,40};
		int[] moondist = {2,3,4,5,6,7,8,8};
		
		moons = new ArrayList<Moon>();
		int noMoons = ran.nextInt(moondist[size]);
		
		
		for(int x=0; x<noMoons; x++){
			moons.add(new Moon(name, x+1, this));
		}
		
		
		String[] potSprite;
		
		if(life){
			potSprite = new File(".//images//celestial//planet//life//").list();
			String tempSprite = potSprite[ran.nextInt(potSprite.length)];
			while(!tempSprite.substring(tempSprite.length()-3).equals("png")&&!tempSprite.substring(tempSprite.length()-3).equals("jpg")){
				//System.out.println("FAIL1 "+tempSprite+": "+tempSprite.substring(tempSprite.length()-3));
				tempSprite = potSprite[ran.nextInt(potSprite.length)];
			}
			sprite=".//images//celestial//planet//life//"+tempSprite;
		}
		else{
			if(temp>2){
				if(ran.nextInt(2)==0){
					potSprite = new File(".//images//celestial//planet//cold//").list();
					String tempSprite = potSprite[ran.nextInt(potSprite.length)];
					while(!tempSprite.substring(tempSprite.length()-3).equals("png")&&!tempSprite.substring(tempSprite.length()-3).equals("jpg")){
						//System.out.println("FAIL2 "+tempSprite+": "+tempSprite.substring(tempSprite.length()-3));
						tempSprite = potSprite[ran.nextInt(potSprite.length)];
					}
					sprite=".//images//celestial//planet//cold//"+tempSprite;
				}
				else{
					potSprite = new File(".//images//celestial//planet//neutral//").list();
					String tempSprite = potSprite[ran.nextInt(potSprite.length)];
					while(!tempSprite.substring(tempSprite.length()-3).equals("png")&&!tempSprite.substring(tempSprite.length()-3).equals("jpg")){
						//System.out.println("FAIL3 "+tempSprite+": "+tempSprite.substring(tempSprite.length()-3));
						tempSprite = potSprite[ran.nextInt(potSprite.length)];
					}
					sprite=".//images//celestial//planet//neutral//"+tempSprite;
				}
			}
			else if(temp<2){
				if(ran.nextInt(2)==0){
					potSprite = new File(".//images//celestial//planet//hot//").list();
					String tempSprite = potSprite[ran.nextInt(potSprite.length)];
					while(!tempSprite.substring(tempSprite.length()-3).equals("png")&&!tempSprite.substring(tempSprite.length()-3).equals("jpg")){
						//System.out.println("FAIL4 "+tempSprite+" "+tempSprite.substring(tempSprite.length()-3));
						tempSprite = potSprite[ran.nextInt(potSprite.length)];
					}
					sprite=".//images//celestial//planet//hot//"+tempSprite;
				}
				else{
					potSprite = new File(".//images//celestial//planet//neutral//").list();
					String tempSprite = potSprite[ran.nextInt(potSprite.length)];
					while(!tempSprite.substring(tempSprite.length()-3).equals("png")&&!tempSprite.substring(tempSprite.length()-3).equals("jpg")){
						//System.out.println("FAIL5 "+tempSprite+" "+tempSprite.substring(tempSprite.length()-3));
						tempSprite = potSprite[ran.nextInt(potSprite.length)];
					}
					sprite=".//images//celestial//planet//neutral//"+tempSprite;
				}
			}
			else{
				potSprite = new File(".//images//celestial//planet//neutral//").list();
				String tempSprite = potSprite[ran.nextInt(potSprite.length)];
				while(!tempSprite.substring(tempSprite.length()-3).equals("png")&&!tempSprite.substring(tempSprite.length()-3).equals("jpg")){
					//System.out.println("FAIL6 "+tempSprite+" "+tempSprite.substring(tempSprite.length()-3));
					tempSprite = potSprite[ran.nextInt(potSprite.length)];
				}
				sprite=".//images//celestial//planet//neutral//"+tempSprite;
			}
		}
		
		switch(size){
		case 0: spriteSize=0.7;break;
		case 1: spriteSize=0.75;break;
		case 2: spriteSize=0.8;break;
		case 3: spriteSize=0.85;break;
		case 4: spriteSize=0.85;break;
		case 5: spriteSize=0.9;break;
		case 6: spriteSize=0.95;break;
		case 7: spriteSize=1.0;break;
		}		
		

		genDesc();
		
	}
	
	public void genDesc(){
		
		desc=SIZE[size]+" "+TEMP[temp]+" ";
		if(life)
			desc+="life sustaining ";		
		desc+=PLANETTYPE[type]+" with "+moons.size()+" significant moons";
		if(rings==1)
			desc+=" and a plantery ring";
		if(rings==2)
			desc+=" and an extensive plantery ring system";
		desc+=".";
		
		desc+=" Orbits "+system.star.name+" at a distance of "+dist+" VUs. ";
		
		switch(size){
		case 0: spriteSize=0.55;break;
		case 1: spriteSize=0.625;break;
		case 2: spriteSize=0.7;break;
		case 3: spriteSize=0.775;break;
		case 4: spriteSize=0.775;break;
		case 5: spriteSize=0.85;break;
		case 6: spriteSize=0.925;break;
		case 7: spriteSize=1.0;break;
		}
	}
	
	public ArrayList<Species> getSpecies(){
		ArrayList<Species> res=new ArrayList<Species>();
		
		for(Species species:system.galaxy.species){
			if(species.origin.equals(this))
				res.add(species);
		}
		
		return res;
	}
	
}